<template>
  <el-dialog
    :title="!bargainOrder.bargainOrderId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="bargainOrder" :rules="dataRule" ref="bargainOrder" @keyup.enter.native="bargainOrderSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="bargainOrder.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="砍价活动id" prop="bargainId">
      <el-input v-model="bargainOrder.bargainId" placeholder="砍价活动id"></el-input>
    </el-form-item>
    <el-form-item label="收获地址" prop="deliveryAddrId">
      <el-input v-model="bargainOrder.deliveryAddrId" placeholder="收获地址"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="goodsName">
      <el-input v-model="bargainOrder.goodsName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="goodsImg">
      <el-input v-model="bargainOrder.goodsImg" placeholder="商品图片"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="goodsPrice">
      <el-input v-model="bargainOrder.goodsPrice" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="最底价" prop="floorPrice">
      <el-input v-model="bargainOrder.floorPrice" placeholder="最底价"></el-input>
    </el-form-item>
    <el-form-item label="已砍刀数" prop="cutCount">
      <el-input v-model="bargainOrder.cutCount" placeholder="已砍刀数"></el-input>
    </el-form-item>
    <el-form-item label="商品状态[0、砍价中 1、已砍成 2、已支付 3、已关闭]" prop="status">
      <el-input v-model="bargainOrder.status" placeholder="商品状态[0、砍价中 1、已砍成 2、已支付 3、已关闭]"></el-input>
    </el-form-item>
    <el-form-item label="订单创建时间" prop="createTime">
      <el-input v-model="bargainOrder.createTime" placeholder="订单创建时间"></el-input>
    </el-form-item>
    <el-form-item label="订单支付时间" prop="payDate">
      <el-input v-model="bargainOrder.payDate" placeholder="订单支付时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="bargainOrderSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        bargainOrder: {
          bargainOrderId: 0,
          userId: '',
          bargainId: '',
          deliveryAddrId: '',
          goodsName: '',
          goodsImg: '',
          goodsPrice: '',
          floorPrice: '',
          cutCount: '',
          status: '',
          createTime: '',
          payDate: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          bargainId: [
            { required: true, message: '砍价活动id不能为空', trigger: 'blur' }
          ],
          deliveryAddrId: [
            { required: true, message: '收获地址不能为空', trigger: 'blur' }
          ],
          goodsName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          goodsImg: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ],
          goodsPrice: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          floorPrice: [
            { required: true, message: '最底价不能为空', trigger: 'blur' }
          ],
          cutCount: [
            { required: true, message: '已砍刀数不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '商品状态[0、砍价中 1、已砍成 2、已支付 3、已关闭]不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '订单创建时间不能为空', trigger: 'blur' }
          ],
          payDate: [
            { required: true, message: '订单支付时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.bargainOrder.bargainOrderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['bargainOrder'].resetFields()
          if (this.bargainOrder.bargainOrderId) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargainorder/info/${this.bargainOrder.bargainOrderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.bargainOrder.userId = data.bargainOrder.userId
                this.bargainOrder.bargainId = data.bargainOrder.bargainId
                this.bargainOrder.deliveryAddrId = data.bargainOrder.deliveryAddrId
                this.bargainOrder.goodsName = data.bargainOrder.goodsName
                this.bargainOrder.goodsImg = data.bargainOrder.goodsImg
                this.bargainOrder.goodsPrice = data.bargainOrder.goodsPrice
                this.bargainOrder.floorPrice = data.bargainOrder.floorPrice
                this.bargainOrder.cutCount = data.bargainOrder.cutCount
                this.bargainOrder.status = data.bargainOrder.status
                this.bargainOrder.createTime = data.bargainOrder.createTime
                this.bargainOrder.payDate = data.bargainOrder.payDate
              }
            })
          }
        })
      },
      // 表单提交
      bargainOrderSubmit () {
        this.$refs['bargainOrder'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargainorder/${!this.bargainOrder.bargainOrderId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'bargainOrderId': this.bargainOrder.bargainOrderId || undefined,
                'userId': this.bargainOrder.userId,
                'bargainId': this.bargainOrder.bargainId,
                'deliveryAddrId': this.bargainOrder.deliveryAddrId,
                'goodsName': this.bargainOrder.goodsName,
                'goodsImg': this.bargainOrder.goodsImg,
                'goodsPrice': this.bargainOrder.goodsPrice,
                'floorPrice': this.bargainOrder.floorPrice,
                'cutCount': this.bargainOrder.cutCount,
                'status': this.bargainOrder.status,
                'createTime': this.bargainOrder.createTime,
                'payDate': this.bargainOrder.payDate
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
